'use strict';

angular.module('fasApp',[
	'ngRoute',
    'ui.bootstrap',
    'tree.dropdown', //下拉列表树
    'com.devnup.alert',// 提示框
    'cn.edu.ncepu.directives.menunav',//左侧菜单控件
    'com.lx.ngsl.user.controller',
    'com.lx.ngsl.user.service',
    'com.lx.ngsl.role.controller',
    'com.lx.ngsl.role.service',
    'com.lx.ngsl.quest.controller',
    'com.lx.ngsl.quest.service',
    'com.lx.ngsl.menu.service',
    'com.lx.ngsl.menu.controller',
    'com.lx.ngsl.autopaper.controller',
    'com.lx.ngsl.autopaper.service',
    'com.lx.ngsl.tjfx.controller',
    'com.lx.ngsl.history.service',
    'com.lx.ngsl.history.controller'
])

//请求拦截器
.factory('requestInterceptor', function() {
	return {
		'request' : function(req){
			//console.log("请求的url：" + req.url);
			//console.log("请求的方式：" + req.method);
			//console.log("请求的Accept头属性：" + req.headers.Accept);//“Accept” 头属性能被浏览器用来指定响应的media 类型，表示自己可以接受的类型。
			return req;
		}
	};
})

//响应拦截器
.factory('responseInterceptor', function() {
        return {
            'responseError': function(response) {
            	if (response.status == '401') {
            		window.location.href = "/ngsl/login.html";
            	}
                return response;
            },
            'response' : function(resp){
				return resp;
			}
        };
    })
   //向后台获取当前登录用户信息的同时触发后台拦截器，判断当前用户是否登录 
  .controller('appController', function($scope, $http, $rootScope) {//'$scope', '$http', 
	  //从后台获取当前登录用户
	  $scope.getUserInfo = function() {
		  $http.get(
					"/ngsl/v0/userInfo", {}
				  ).success(function(data, status) {
					    $scope.userInfo = {};
						$scope.userInfo.name = data.name;
						$scope.userInfo.privatetoken = data.privatetoken;
						var token = $scope.userInfo.privatetoken;
					}).error(function(data, status) {
	  					alert(data.error);
						$scope.data = data || "Request failed";
						$scope.status = status;
			});
	  }
	 $scope.getUserInfo();
  })
    
.config(function ($routeProvider, $httpProvider) {//'$routeProvider', '$httpProvider',
	$httpProvider.interceptors.push('requestInterceptor');//将拦截器注册到config中
	$httpProvider.interceptors.push('responseInterceptor');
	
	//用户列表
	$routeProvider.when('/user/list', {
        templateUrl: 'modules/user/views/user_list.html',
        controller: 'userListController'
      });
	
	//用户新增
	$routeProvider.when('/user/add', {
        templateUrl: 'modules/user/views/user_add.html',
        controller: 'userAddController'
      });
	//用户修改
	$routeProvider.when('/user/edit', {
		templateUrl: 'modules/user/views/user_edit.html',
		controller: 'userEditController'
	});
	
	//角色列表
	$routeProvider.when('/role/list', {
		templateUrl: 'modules/role/views/role_list.html',
		controller: 'roleListController'
	});
	
	//角色新增
	$routeProvider.when('/role/add', {
		templateUrl: 'modules/role/views/role_add.html',
		controller: 'roleAddController'
	});
	//角色修改
	$routeProvider.when('/role/edit', {
		templateUrl: 'modules/role/views/role_edit.html',
		controller: 'roleEditController'
	});
	//试题列表
	$routeProvider.when('/quest/list', {
		templateUrl: 'exam/quest/views/quest_list.html',
		controller: 'questListController'
	});
	
	//试题新增
	$routeProvider.when('/quest/add', {
		templateUrl: 'exam/quest/views/quest_add.html',
		controller: 'questAddController'
	});
	//试题修改
	$routeProvider.when('/quest/edit', {
		templateUrl: 'exam/quest/views/quest_edit.html',
		controller: 'questEditController'
	});
	//菜单&功能
	$routeProvider.when('/menu/edit', {
		templateUrl: 'modules/menu_edit/views/menu_edit.html',
		controller: 'menuEditController'
	});
	//欢迎页面
	$routeProvider.when('/', {
		templateUrl: 'welcome.html',
		//controller: 'progressController'
	});
	$routeProvider.when('/test_manage/paper', {
		templateUrl: 'exam/autopaper/views/autopaper.html',
		controller: 'autopaperController'
	});
	//统计分析
	$routeProvider.when('/tjfx', {
		templateUrl: 'exam/tjfx/views/tjfx.html',
		controller: 'tjfxController'
	});
	$routeProvider.when('/grade/list', {
		templateUrl: 'exam/history/views/history_list.html',
		controller: 'historyController'
	});
//	.otherwise({
//        redirectTo: '/a'
//      });
	
});
  
